package com.fsmcache.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/*******************
 *@Describe 个人实时缓存服务器
 *@author 封神梦
 *@Time 2022/4/14
 *@Company
 ****************/
public class FsmCacheUtils {

    /**
     * 使用数据模型做键值
     */
    static ConcurrentHashMap<Class, HashMap<String, Object>> g_HashData = new ConcurrentHashMap();
    /**
     * 使用String做键值
     */
    static ConcurrentHashMap<String, HashMap<String, Object>> g_StringHashData = new ConcurrentHashMap();
    /**
     * 常规k-v模型
     */
    static ConcurrentHashMap<String, String> g_StringData = new ConcurrentHashMap();

    /**
     * @param key
     * @param fileid
     * @param data
     * @return
     */
    public static Integer hset(Class key, String fileid, Object data) {
        HashMap<String, Object> dataMap = g_HashData.get(key);
        if (dataMap == null) {
            dataMap = new HashMap<>();
            dataMap.put(fileid, data);
            g_HashData.put(key, dataMap);
        } else {
            dataMap.put(fileid, data);
        }

        return dataMap.size();
    }

    public static Integer hset(Class key, HashMap<String, Object> data) {
        g_HashData.put(key, data);
        return g_HashData.size();
    }

    public static <T> T hget(Class<T> key, String fileid) {
        try {
            HashMap<String, Object> dataMap = g_HashData.get(key);
            if (dataMap != null) {
                Object ret = dataMap.get(fileid);
                return ret == null ? null : (T) ret;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }


    //获取数据列表
    public static <T> List<T> hget(Class key) {
        HashMap<String, Object> dataMap = g_HashData.get(key);
        try {
            if (dataMap != null) {
                return (List<T>) dataMap.values().stream().collect(Collectors.toList());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 删除一条数据
     *
     * @param key
     * @param filedid
     */
    public static void hdel(Class key, String filedid) {
        HashMap<String, Object> dataMap = g_HashData.get(key);
        if (dataMap != null) {
            dataMap.remove(filedid);
        }
    }

    /**
     * 清除一组数据
     *
     * @param key
     */
    public static void hclear(Class key) {
        HashMap<String, Object> dataMap = g_HashData.get(key);
        if (dataMap != null) {
            dataMap.clear();
        }
    }

    /**
     * 修改数据
     *
     * @param key    键值
     * @param fileid 数据id
     * @param data   数据内容
     * @return 该组数据的数量
     */
    public static Integer hset(String key, String fileid, Object data) {
        HashMap<String, Object> dataMap = g_StringHashData.get(key);
        if (dataMap == null) {
            dataMap = new HashMap<>();
            dataMap.put(fileid, data);
            g_StringHashData.put(key, dataMap);
        } else {
            dataMap.put(fileid, data);
        }

        return dataMap.size();
    }

    /**
     * 修改一组数据内容
     *
     * @param key
     * @param data
     * @return
     */
    public static Integer hset(String key, HashMap<String, Object> data) {
        g_StringHashData.put(key, data);
        return g_StringHashData.size();
    }

    /**
     * 获取数据内容
     *
     * @param key    String类型键值
     * @param fileid 数据id
     * @param <T>    数据类型
     * @return
     */
    public static <T> T hget(String key, String fileid) {
        HashMap<String, Object> dataMap = g_StringHashData.get(key);
        try {
            if (dataMap != null) {
                Object ret = dataMap.get(fileid);
                return ret == null ? null : (T) ret;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }


    public static <T> List<T> hget(String key) {
        HashMap<String, Object> dataMap = g_StringHashData.get(key);
        if (dataMap != null) {
            try {
                return (List<T>) dataMap.values().stream().collect(Collectors.toList());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return new ArrayList<>();
    }


    public static void hdel(String key, String filedid) {
        HashMap<String, Object> dataMap = g_StringHashData.get(key);
        if (dataMap != null) {
            dataMap.remove(filedid);
        }
    }

    public static void hclear(String key) {
        g_StringHashData.remove(key);
    }

    public static Integer set(String key, String data) {
        g_StringData.put(key, data);
        return g_StringData.size();
    }

    public static String get(String key) {
        return g_StringData.get(key);
    }

    public static void del(String key) {
        g_StringData.remove(key);
    }

    public static void clear() {
        g_StringData.clear();
    }

}
